/*
 * @Description 
 * @Autor lzhiqiang
 * @Date 2023-02-25 13:26:09
 * @LastEditors lzhiqiang
 * @LastEditTime 2023-03-06 15:16:43
 */
#include <iostream>
#include "johnsonli.h"

johnsonli::Logger::ptr g_logger = LOG_ROOT();

void run_in_idle()
{
	LOG_INFO(g_logger) << "run_in_idle";
}


void run_in_fiber()
{
	LOG_INFO(g_logger) << "run_in_fiber begin";
	/*johnsonli::Fiber::YieldToHold();*/

	//协程里面不开协程，统一在线程中开协程
	/*johnsonli::Fiber::ptr idle(new johnsonli::Fiber(run_in_idle));
	idle->call();*/
	

	LOG_INFO(g_logger) << "run_in_fiber end";
}

void test_fiber()
{
	johnsonli::Fiber::GetThis(); // 会创建一个主协程
	LOG_INFO(g_logger) << "main begin";
	{
		johnsonli::Fiber::ptr fiber(new johnsonli::Fiber(run_in_fiber));

		fiber->call();
		LOG_INFO(g_logger) << "main after swapIn";
		LOG_INFO(g_logger) << "main end1";
	}

	LOG_INFO(g_logger) << "main end2";
}

int main()
{
	johnsonli::Thread::SetName("main");
	test_fiber();
	//std::vector<johnsonli::Thread::ptr> thrs;
	//for (int i = 0; i < 3; ++i)
	//{
	//	thrs.push_back(johnsonli::Thread::ptr(new johnsonli::Thread(&test_fiber, "name_" + std::to_string(i))));
	//}

	//for (auto& i : thrs)
	//{
	//	i->join();
	//}
	//getchar();//sdad
	return 0;
}